import { createRouter, createWebHistory } from 'vue-router'
import Layout from '../views/Layout.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      component: Layout,
      children: [
        {
          path: '',
          name: 'upload',
          component: () => import('../views/index/index.vue'),
          meta: {
            title: '图片上传'
          }
        },
        {
          path: 'gallery',
          name: 'gallery',
          component: () => import('../views/index/gallery.vue'),
          meta: {
            title: '图片管理'
          }
        },
        {
          path: 'settings',
          name: 'settings',
          component: () => import('../views/index/settings.vue'),
          meta: {
            title: '系统设置'
          }
        }
      ]
    },
    {
      path: '/:pathMatch(.*)*',
      redirect: '/'
    }
  ],
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 图床系统` : '图床系统'
  next()
})

export default router
